package lru;


import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class LRUCacheDemo<K, V> extends LinkedHashMap<K, V> {

    // 缓存容量
    private final int capacity;

    public LRUCacheDemo(int capacity) {
        // accessOrder：the ordering mode. true for access-order；false for insertion-order
//        当 accessOrder = true 时，每次使用 key 时（put 或者 get 时），都将 key 对应的数据移动到队尾（右边），
//        表示最近经常使用；当 accessOrder = false 时，key 的顺序为插入双向链表时的顺序
        super(capacity, 0.75F, false);
        this.capacity = capacity;
    }


//     用于判断是否需要删除最近最久未使用的节点
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return super.size() > capacity;
    }

    public static void main(String[] args) {
        LRUCacheDemo lruCacheDemo = new LRUCacheDemo(3);
        lruCacheDemo.put(1, "a");
        lruCacheDemo.put(2, "b");
        lruCacheDemo.put(3, "c");
//        System.out.println(lruCacheDemo.keySet());

        Set<Map.Entry<String, String>> set = lruCacheDemo.entrySet();
        Iterator<Map.Entry<String, String>> iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + "\t");
        }
        System.out.println();

        lruCacheDemo.put(4, "d");
        set = lruCacheDemo.entrySet();
        iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + "\t");
        }
        System.out.println();


//        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(3, "e");
        set = lruCacheDemo.entrySet();
        iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + "\t");
        }
        System.out.println();


//        System.out.println(lruCacheDemo.keySet());


//        lruCacheDemo.put(3, "c");
//        System.out.println(lruCacheDemo.keySet());
//        lruCacheDemo.put(3, "e");
//        System.out.println(lruCacheDemo.keySet());
//        lruCacheDemo.put(5, "x");
//        System.out.println(lruCacheDemo.keySet());


//            LinkedHashMap<String, String> linkedHashMap =
//                    new LinkedHashMap<String, String>(16, 0.75f, true);
//            linkedHashMap.put("111", "111");
//            linkedHashMap.put("222", "222");
//            linkedHashMap.put("333", "333");
//            linkedHashMap.put("444", "444");
//            loopLinkedHashMap(linkedHashMap);
//            linkedHashMap.get("111");
//            loopLinkedHashMap(linkedHashMap);
//            linkedHashMap.put("222", "2222");
//            loopLinkedHashMap(linkedHashMap);
    }

    public static void loopLinkedHashMap(LinkedHashMap<String, String> linkedHashMap) {
        Set<Map.Entry<String, String>> set = linkedHashMap.entrySet();
        Iterator<Map.Entry<String, String>> iterator = set.iterator();

        while (iterator.hasNext()) {
            System.out.print(iterator.next() + "\t");
        }
        System.out.println();
    }
}


